<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Core_Config {
	
	protected $_data = null;
	
	protected function _initData() {
		if (!$this->_data) {
			$rows = App::db()->query('select * from config')->result_array();
			$this->_data = array();
			foreach($rows as $r)
				$this->_data[$r['name']] = $r['value'];
		}
	}

	public function set($name, $value) {
		$this->_initData();
		// update
		if (isset($this->_data[$name]))
			$sql = "UPDATE config SET value = '$value' WHERE name = '$name'";
		else	// insert
			$sql = "INSERT INTO config(name, value) VALUES ('$name', '$value')";
		App::db()->query($sql);
		$this->_data[$name] = $value;
	}
	
	public function get($name) {
		$this->_initData();
		return isset($this->_data[$name]) ? $this->_data[$name] : null;
	}
	
}